const organization = this.pageObj.getApiWidgetByAliasName('org_tree');
const three = this.pageObj.getApiWidgetByAliasName('三维组件');
const search_input = this.pageObj.getApiWidgetByAliasName('search_input');
const [ select ] = $event;
const value = select.value;
const artemis = three.widgetObj.artemis;

// console.log($event);
// console.log(organization.widgetObj._option.dataSource)

function breakDataSource(item) {
  if (item.personId) {
    item.title = item.accountName || "默认name";
    item.userId = item.account;
    item.treeId = item.id;
  } else if(item.orgId || item.orgName) {
    item.title = item.orgName;
    item.id = item.orgId;
    item.treeId = item.orgId;
  } else if (item.deptId || item.deptName) {
    item.title = item.deptName;
    item.id = item.deptId;
    item.treeId = item.deptId;
  }

  item.type = '1' // 叶子节点标识
  if (item.values) {
    item.children = item.values.map(breakDataSource);
    item.type = '0'
  }
  if (item.child) {
    item.children = item.child.map(breakDataSource);
    item.type = '0'
  }

  return item
}


const accountName = $(search_input.__box).find('.input-filter').val() || '';
fetch(`/Communication/classroom/selectAllStructure`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': '85502d3a1fb9de6967e801081a0b3056',
  },
  body: JSON.stringify({ accountName }),
}).then(res => res.json()).then(({ result, success }) => {
  console.log(result, success)
  if (success === true) {
    const dataSource = result.map(breakDataSource)

    if (value === 'online') {
      organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource);
    } else {
      organization.widgetObj.option_data = artemis.filterUsersOnline(dataSource, true);
    }

    organization.widgetObj.render()
    console.log(dataSource)
  }
})